package main

import (
	"fmt"
	"reflect"
	"strconv"
)

// TheBaseType 基本类型转文本
func TheBaseType(v interface{}) (string, string) {
	switch V := v.(type) {
	// 数值类型
	case int:
		return "int", strconv.FormatInt(int64(V), 10)
	case int8:
		return "int8", strconv.FormatInt(int64(V), 10)
	case int16:
		return "int16", strconv.FormatInt(int64(V), 10)
	case int32:
		return "int32", strconv.FormatInt(int64(V), 10)
	case int64:
		return "int64", strconv.FormatInt(V, 10)
	// 整数类型
	case uint:
		return "uint", strconv.FormatUint(uint64(V), 10)
	case uint8:
		return "uint8", strconv.FormatUint(uint64(V), 10)
	case uint16:
		return "uint16", strconv.FormatUint(uint64(V), 10)
	case uint32:
		return "uint32", strconv.FormatUint(uint64(V), 10)
	case uint64:
		return "uint64", strconv.FormatUint(V, 10)
	// 浮点数类型
	case float32:
		return "float32", strconv.FormatFloat(float64(V), 'f', 2, 64)
	case float64:
		return "float64", strconv.FormatFloat(V, 'f', 2, 64)
	// 逻辑型
	case bool:
		if V {
			return "bool", "true"
		}
		return "bool", "false"
	// 文本字节型
	case string:
		return "string", V
	// 其他类型
	default:
		return TheArrayType(v)
	}
}

// TheArrayType 输出基本的数组类型
func TheArrayType(v interface{}) (Type string, Str string) {
	// 不是基本类型
	Str = ""
	Type = reflect.TypeOf(v).String()
	// 退出处理
	defer func() {
		if Str != "" {
			Str = "[" + Str[1:] + "]"
		}
	}()
	// 处理数组
	switch Var := v.(type) {
	case []int:
		for _, d := range Var {
			Str += "," + strconv.FormatInt(int64(d), 10)
		}
	case []int8:
		for _, d := range Var {
			Str += "," + strconv.FormatInt(int64(d), 10)
		}
	case []int16:
		for _, d := range Var {
			Str += "," + strconv.FormatInt(int64(d), 10)
		}
	case []int32:
		for _, d := range Var {
			Str += "," + strconv.FormatInt(int64(d), 10)
		}
	case []int64:
		for _, d := range Var {
			Str += "," + strconv.FormatInt(d, 10)
		}
	case []uint:
		for _, d := range Var {
			Str += "," + strconv.FormatUint(uint64(d), 10)
		}
	case []uint8:
		for _, d := range Var {
			Str += "," + strconv.FormatUint(uint64(d), 10)
		}
	case []uint16:
		for _, d := range Var {
			Str += "," + strconv.FormatUint(uint64(d), 10)
		}
	case []uint32:
		for _, d := range Var {
			Str += "," + strconv.FormatUint(uint64(d), 10)
		}
	case []uint64:
		for _, d := range Var {
			Str += "," + strconv.FormatUint(d, 10)
		}
	case []float32:
		for _, d := range Var {
			Str += "," + strconv.FormatFloat(float64(d), 'f', 2, 64)
		}
	case []float64:
		for _, d := range Var {
			Str += "," + strconv.FormatFloat(d, 'f', 2, 64)
		}
	case []bool:
		for _, d := range Var {
			if d {
				Str += "," + "true"
			} else {
				Str += "," + "false"
			}
		}
	case []string:
		for _, d := range Var {
			Str += "," + d
		}
	// 其他类型
	default:
		Str = ""
	}
	return
}

func main() {
	fmt.Println(TheBaseType(1))
	fmt.Println(TheBaseType(false))
	fmt.Println(TheBaseType("TheBaseType([]int{1, 2, 3, 4, 5})"))
	fmt.Println(TheBaseType([]int{1, 2, 3, 4, 5}))
}
